package com.wkf.truck.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkf.truck.common.CommonEnum;
import com.wkf.truck.common.R;
import com.wkf.truck.model.Favorite;
import com.wkf.truck.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lxb
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;


    /**
     *
     * @param userId
     * @param truckId
     * @return
     */
    @GetMapping("/check")
    public R checkFavorite(@RequestParam("userId") Long userId, @RequestParam("truckId") Long truckId) {

        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("truck_id", truckId);
        wrapper.eq("status","1");
        Favorite favorite = favoriteService.getOne(wrapper);

        R r = new R();
        //未收藏
        if (favorite == null) {
            r.put("msg","为收藏");
            r.put("status",false);
            r.put("code", 200);
        }
        //已收藏
        else {
            r.put("msg", "已收藏");
            r.put("status",true);
            r.put("code", 200);
            return r;
        }
        return r;
    }


    /**
     * 货车收藏
     *
     * @param userId
     * @param truckId
     * @return
     */
    @GetMapping("/getCollect")
    public R collectTruck(@RequestParam("userId") Long userId, @RequestParam("truckId") Long truckId, @RequestParam("status") String status) {
        R r = favoriteService.collectTruck(userId, truckId, status);
        return r;
    }

    /**
     * 获取用户收藏列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/getCollectLists")
    public R list(@RequestParam("userId") Long userId) {
        QueryWrapper<Favorite> wrapper = new QueryWrapper<Favorite>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", CommonEnum.Favorite.收藏.getCode());
        List<Favorite> list = favoriteService.list(wrapper);
        return R.ok().put("data", list);
    }
}
